package com.central.apps.handler.project;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.util.SysUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理项目文档管理业务逻辑
 *
 * @author lidachao
 * @date 2022/9/28
 */
@Component
@Slf4j
public class ProjectDocumentHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        if("capture".equals(flowNode.getId())) {
            // 查询附件信息
            List<Map<String, Object>> docList = handlerService.findBySql("select fileid from tb_file where appname = '" + app + "' and recordid = " + id);
            if (CollectionUtils.isEmpty(docList)) {
                return "上传交付物文档信息不能为空";
            } else {
                if (docList.size() > 1) {
                    return "上传交付物文档信息不能上传多份";
                }
            }
        }

        if(flowNode.getId().equals("complete") && op.isFlag()) {
            // 查询文档管理信息
            List<Map<String, Object>> pdList = handlerService.findBySql("select * from gq_prodocument where id = " + id);
            Map<String, Object> pdMap = pdList.get(0);
            Integer proId = MapUtils.getInteger(pdMap, "proid");
            Integer taskId = MapUtils.getInteger(pdMap, "taskid");
            Integer taskDelId = MapUtils.getInteger(pdMap, "taskdelid");
            String taskDelName = MapUtils.getString(pdMap, "taskdelname");

            if (taskDelId != null) {
                // 更新任务交付物状态
                handlerService.updateBySql("update gq_taskdeliverables set status = '已交付' where status = '未交付' and proid = " + proId + " and id = " + taskDelId);
            }

            // 插入交付物版本信息
            List<Map<String, Object>> docList = handlerService.findBySql("select fileid, fileversion, filerealname from tb_file where appname = '" + app + "' and recordid = " + id);
            Map<String, Object> docMap = docList.get(0);
            Integer fileid = MapUtils.getInteger(docMap, "fileid");
            String fileRealName = MapUtils.getString(docMap, "filerealname");

            Map<String, Object> docFileMap = new HashMap<>();
            docFileMap.put("pdid", id);
            docFileMap.put("subjectno", MapUtils.getString(pdMap, "subjectno"));
            docFileMap.put("proid", proId);
            docFileMap.put("taskid", taskId);
            docFileMap.put("taskname", MapUtils.getString(pdMap, "taskname"));
            docFileMap.put("taskdelid", MapUtils.getString(pdMap, "taskdelid"));
            docFileMap.put("taskdelname", MapUtils.getString(pdMap, "taskdelname"));
            docFileMap.put("deltypeid", MapUtils.getString(pdMap, "deltypeid"));
            docFileMap.put("deltype", MapUtils.getString(pdMap, "deltype"));
            docFileMap.put("delcategoryid", MapUtils.getString(pdMap, "delcategoryid"));
            docFileMap.put("delcategory", MapUtils.getString(pdMap, "delcategory"));
            docFileMap.put("fileid", fileid);
            docFileMap.put("filerealname", fileRealName);
            docFileMap.put("createuserdesc", SysUserUtil.getLoginAppUser().getNickname());

            StringBuilder sql = new StringBuilder();
            sql.append("select id from gq_prodocument where status = '已批准' and proid = " + proId + " and taskid = " + taskId);

            if (taskDelId != null) {
                sql.append(" and taskdelid = " + taskDelId);
            } else {
                sql.append(" and taskdelname = '" + taskDelName + "'");
            }

            List<Map<String, Object>> list = handlerService.findBySql(sql.toString());

            if (CollectionUtils.isEmpty(list)) {
                docFileMap.put("docversion", "V" + 1);
            } else {
                docFileMap.put("docversion", "V" + (list.size() + 1));
            }

            handlerService.insert("gq_prodocfile", docFileMap);
        }
        return null;
    }
}
